package com.frozenc.controller.comments;

import com.frozenc.domain.Comment;
import com.frozenc.domain.User;
import com.frozenc.mapper.CommentsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * Created by FrozenC on 2016/9/20.
 */
@Controller
public class commentsController {

    @Autowired
    private CommentsMapper commentsMapper;

    @ModelAttribute
    public void setUser(HttpSession session, ModelMap modelMap){
        User user = (User) session.getAttribute("currUser");
        modelMap.addAttribute("currUser",user);
        return ;
    }

    @RequestMapping(value = "/comment",method = RequestMethod.POST)
    @ResponseBody
    String postComment(@RequestBody Comment comment,ModelMap modelMap){

        User user = (User) modelMap.get("currUser");

        if(user==null){
            return "未登录！";
        }

        int res = commentsMapper.addComments(comment.getNewsId(),user.getId(),comment.getComment());

        System.out.println(res);

        return "ok";
    }
}
